package com.banshou.installs.utils;

import android.text.TextUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by cjq on 2018/4/11.
 * Email: stoic_yb@139.com
 * features: 参数输入对象
 */
public class ParmasUtils {
    public static <T> Map<String, String> objectMap(T t) {
        Map<String, String> map = new HashMap<>();
        Class clazz = t.getClass();
        //屏蔽Object类的影响
        while (clazz != null && !clazz.getName().toLowerCase().equals("java.lang.object")) {
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Field field : declaredFields) {
                //获取private属性的变量
                field.setAccessible(true);
                try {
                    if (!TextUtils.isEmpty(field.get(t).toString())) {
                        map.put(field.getName(), field.get(t).toString());
                    } else {
                        map.put(field.getName(), "");
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return map;
    }
}
